layui.config({
    version: '1510164124181'
}).use(['form', 'laydate', 'laypage', 'layer', 'element'], function () {
    form = layui.form;
    layer = layui.layer;
    laydate = layui.laydate; //日期
    laypage = layui.laypage; //分页
    element = layui.element; //元素操作
    pageNo = GetQueryString("pageNo");
    if (!isNotNull(pageNo)) pageNo = 1;
    TypelistByTypeId(2); //查询所有商品类型
    myrefresh();
});

function ProductListExport() {
    var identify = "";
    var type = $("#types option:selected").val();
    var actType = $("#actTypes option:selected").val();
    var title = $("#inputname").val();
    var pid = $("#inputpid").val();
    window.location.href = httpurl + "/ProductListExport?title=" + title + "&identify=" + identify + "&type=" + type + "&pid=" + pid + "&actType=" + actType;
    // $.ajax({
    //     url: httpurl + "/ProductListExport",
    //     headers: {
    //         "Access-Control-Allow-Origin": "*",
    //         "Access-Control-Allow-Methods": "*",
    //         "Access-Control-Allow-Headers": "Authorization",
    //         "Authorization": GetTokenInfo()
    //     },
    //     contentType: 'application/json',
    //     responseType: "blob",
    //     cache: false,
    //     data: {
    //         "title": title,
    //         "type": type,
    //         "identify": identify,
    //         "pid": pid
    //     },
    //     success: (res) => {
    //         console.log(res);
    //         // const blob = new Blob([res.data], {
    //         //     type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8"
    //         // });
    //         // console.log(blob);
    //         // let link = document.createElement('a');
    //         // link.href = URL.createObjectURL(blob);
    //         // link.download = "file";
    //         // link.setAttribute('download', '导出文件.xlsx');
    //         // link.click();
    //         // link = null;

    //         const content = res.data;
    //         const blob = new Blob([content], {
    //             type:
    //                 "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8",
    //         });
    //         const elink = document.createElement("a");
    //         elink.setAttribute('download', '导出文件.xlsx');
    //         elink.style.display = "none";
    //         elink.href = URL.createObjectURL(blob);
    //         document.body.appendChild(elink);
    //         elink.click();
    //         URL.revokeObjectURL(elink.href);
    //         document.body.removeChild(elink);
    //     }
    // })
}

function myrefresh() {
    var typeid = $("#types option:selected").val();
    var actType = $("#actTypes option:selected").val();
    var inputname = $("#inputname").val();
    var identify = $("#identify").val();
    var pid = $("#inputpid").val();

    pageNo = (isNotNull(pageNo) ? pageNo : 1);
    $.ajax({
        url: httpurl + "/Productlistpc",
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        cache: false,
        data: {
            "pageNo": pageNo,
            "pageSize": pageSize,
            "typeid": typeid,
            "inputname": inputname,
            "pid": pid,
            identify, actType
        },
        success: function (data) {
            top.layer.closeAll();
            if (data.rtnCode == 200) {
                totalRecords = data.data.total;
                HandleData(data.data.list);
            } else {
                totalRecords = 0;
                let data_list = '<tr>' +
                    '<td colspan="12" style="text-align:center;">暂无数据</td>' +
                    '</tr>';
                $("#tableInfo tbody").html(data_list);
            }
            pageLayPage("myrefresh");
        }
    });
}

function HandleData(initdata) {
    let dataHtml = "";
    for (let i = 0; i < initdata.length; i++) {
        let goodsShowModule = ''
        if(initdata[i].productEntity.isnew){goodsShowModule+='人气好茶'+'/'}
        if(initdata[i].productEntity.iscrux){goodsShowModule+='收藏佳品'+'/'}
        if(initdata[i].productEntity.isrecomm){goodsShowModule+='送礼首选'+'/'}
        if(initdata[i].productEntity.isrecommtu){goodsShowModule+='猜你喜欢'+'/'}
        if(initdata[i].productEntity.discount){goodsShowModule+='折扣商品'+'/'}
        if(initdata[i].productEntity.homeRecommend){goodsShowModule+='今日推荐'+'/'}
        goodsShowModule = goodsShowModule.substring(0,goodsShowModule.length-1)
        dataHtml += '<tr>' +
            '<td><input type="checkbox" data-id="' + initdata[i].productEntity.id + '" name="box" class="aceChild"/></td>' +
            '<th>' + initdata[i].productEntity.id + '</th>' +
            '<th>' + initdata[i].productEntity.identify + '</th>' +
            '<th>' + initdata[i].productEntity.title + '</th>' +
            '<td>' + (initdata[i].productEntity.typeEntity == null ? "" : initdata[i].productEntity.typeEntity.name) + '</td>' +
            '<th>' + goodsShowModule + '</th>' +
            '<td class="width20"><img id="preview" class="preview" src="' + visitimgurl + '/' + (initdata[i].productEntity == null ? "" : initdata[i].productEntity.headimg) + '" height="100" /></td>' +
            '<td>' + initdata[i].productEntity.intime + '</td>' +
            '<td>' + (initdata[i].productEntity.isshelves == "0" ? "否" : "是") + '</td>' +
            '<td colspan="2">' +
            '<table class="width100"><tbody>';
        let dataInfo = initdata[i].setMealEntity;
        for (let a = 0; a < dataInfo.length; a++) {
            dataHtml += '<tr><td class="width50">' + dataInfo[a].meal + '</td><td class="50%">' + dataInfo[a].stock + '</td></tr>';
        }
        dataHtml += '</tbody></table>' +
            '</td>' +
            '<td>' +
            '<a href="#" onclick="update_product_state(\'' + initdata[i].id + '\',\'' + initdata[i].productEntity.isshelves + '\')">' + (initdata[i].productEntity.isshelves == '0' ? "上架" : "下架") + '</a>&nbsp;&nbsp;' +
            '<a href="#" onclick="add_product(' + initdata[i].productEntity.id + ')">编辑</a>&nbsp;&nbsp;' +
            //'<a href="#" data-info="DeleteProductList" data-id="'+initdata[i].productEntity.id+'"  onclick="DeleteInfo(this)">删除</a>' +
            '</td>' +
            '</tr>';
    }
    $("#tableInfo tbody").html(dataHtml);
}
//编辑商品
function add_product(pid) {
    window.location.href = "add_product.html?productid=" + pid + "&pageNo=" + pageNo;
}

function update_product_state(product_id, isshelves) {
    top.layer.confirm('确认提交？', { icon: 3, title: '提示信息' }, function (index) {
        var index = top.layer.msg('提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
        $.ajax({
            type: "POST",
            url: httpurl + "/UpdateProductState",
            cache: false,
            data: JSON.stringify({
                "id": product_id,
                "state": isshelves,
            }),
            headers: {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods": "*",
                "Access-Control-Allow-Headers": "Authorization",
                "Authorization": GetTokenInfo()
            },
            contentType: 'application/json',
            dataType: "json",
            success: function (data) {
                layer.closeAll();
                top.layer.msg("" + data.msg + "");
                if (data.rtnCode == 200) {
                    myrefresh();
                }

            }
        });
    }, function () {

    });
}

function search() {
    pageNo = 1;
    isjump = 0;
    myrefresh();
}